
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Java,Junit,Jacoco," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>junit-jacoco代码覆盖测试 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        junit-jacoco代码覆盖测试
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Jacoco/" rel="tag">Jacoco</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Java/" rel="tag">Java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Junit/" rel="tag">Junit</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 8 分钟</span>
    </header>

    <div class="post-content">
      <h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><h4 id="所需软件、jar"><a href="#所需软件、jar" class="headerlink" title="所需软件、jar"></a>所需软件、jar</h4><ul>
<li>Junit，单元测试依赖<a target="_blank" rel="noopener" href="http://junit.org/">Junit</a></li>
<li>jacoco，用于检测覆盖率，官方网站<a target="_blank" rel="noopener" href="https://www.jacoco.org/jacoco/">https://www.jacoco.org/jacoco/</a>，下载jar包</li>
<li>eclipse，在elipse上安装覆盖率运行的插件<a target="_blank" rel="noopener" href="https://www.jacoco.org/installation.html">EclEmma</a>，在eclipse的 应用商店直接搜索<code>EclEmma</code>，第一个就是。安装成功之后在java类右键有一个选项是<code>Coverage As</code>。</li>
</ul>
<h3 id="创建java项目"><a href="#创建java项目" class="headerlink" title="创建java项目"></a>创建java项目</h3><p><img src="/image/blog-image/18120601.png" alt="项目结构"></p>
<p><code>src/java</code>和<code>test/java</code>都是资源目录。</p>
<h3 id="增加依赖jar"><a href="#增加依赖jar" class="headerlink" title="增加依赖jar"></a>增加依赖jar</h3><p>在项目根目录下新建目录<code>jacocolib</code>。复制从Junit、jacoco官网下载的jar文件。文件如下：</p>
<ul>
<li>hamcrest-core-1.3.jar</li>
<li>jacocoant.jar</li>
<li>junit-4.12.jar</li>
</ul>
<p>这些jar可以直接从我的项目中下载，地址<a target="_blank" rel="noopener" href="https://github.com/ubuntuvim/coverage/tree/master/jacocolib">https://github.com/ubuntuvim/coverage/tree/master/jacocolib</a></p>
<h3 id="编写简单的java代码"><a href="#编写简单的java代码" class="headerlink" title="编写简单的java代码"></a>编写简单的java代码</h3><p>如下是我的业务代码，一些简单的判断。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ubuntuvim.coverage;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 覆盖率</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> ubuntuvim</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Coverage</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">normal</span><span class="params">(<span class="keyword">int</span> type)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (type == <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;one&quot;</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;other&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">exceptMethod</span><span class="params">(<span class="keyword">int</span> p)</span> </span>&#123;</span><br><span class="line">        p = (p + <span class="number">10</span>) / p;</span><br><span class="line">        <span class="keyword">return</span> p;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">throwExcept</span><span class="params">(<span class="keyword">int</span> p)</span> <span class="keyword">throws</span> MyException </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            p = (p + <span class="number">10</span>) / p;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> MyException(<span class="string">&quot;0不能做分母。&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> p;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>自定义的异常类。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ubuntuvim.coverage;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyException</span> <span class="keyword">extends</span> <span class="title">Exception</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">1L</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MyException</span><span class="params">(String message)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(message);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>代码没什么好说的了，非常简单。关键看单元测试方法。</p>
<h3 id="编写单元测试"><a href="#编写单元测试" class="headerlink" title="编写单元测试"></a>编写单元测试</h3><p>下面的单元测试是针对<code>Coverage.java</code>类中三个方法<code>normal</code>、<code>exceptionMethod</code>和<code>throwException</code>的单元测试。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ubuntuvim.coverage;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.junit.Assert.assertEquals;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Before;</span><br><span class="line"><span class="keyword">import</span> org.junit.Rule;</span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> org.junit.rules.ExpectedException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CoverageTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Rule</span></span><br><span class="line">    <span class="keyword">public</span> ExpectedException thrown = ExpectedException.none();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Coverage c = <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Before</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUp</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        c = <span class="keyword">new</span> Coverage();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Test</span>  <span class="comment">// 测试type为1的情况</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testNormalOne</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String s = c.normal(<span class="number">1</span>);</span><br><span class="line">        assertEquals(<span class="string">&quot;one&quot;</span>, s);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span>  <span class="comment">// 测试type为2的情况</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testNormalOther</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String s = c.normal(<span class="number">2</span>);</span><br><span class="line">        assertEquals(<span class="string">&quot;other&quot;</span>, s);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span>  <span class="comment">// 测试正常的情况 （try语句块里的代码）</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testExceptMethodP</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i = c.exceptMethod(<span class="number">10</span>);</span><br><span class="line">        assertEquals(<span class="number">2</span>, i);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 测试出现异常的情况（catch语句块里的代码）</span></span><br><span class="line">    <span class="meta">@Test(expected = ArithmeticException.class)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testExceptMethod</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        c.exceptMethod(<span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span>  <span class="comment">// 测试正常的情况</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testThrowExceptP</span><span class="params">()</span> <span class="keyword">throws</span> MyException </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i = c.throwExcept(<span class="number">10</span>);</span><br><span class="line">        assertEquals(<span class="number">2</span>, i);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Test</span>  <span class="comment">// 测试出现异常的情况</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testThrowExcept</span><span class="params">()</span> <span class="keyword">throws</span> MyException </span>&#123;</span><br><span class="line">        <span class="comment">// 出现异常的情况会返回MyException异常</span></span><br><span class="line">        thrown.expect(MyException.class);</span><br><span class="line">        thrown.expectMessage(<span class="string">&quot;0不能做分母。&quot;</span>);</span><br><span class="line">        c.throwExcept(<span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>有关Junit的使用请自行学习。<br>为了达到100%覆盖，需要对<code>normal</code>、<code>exceptionMethod</code>和<code>throwException</code>这三个方法中每一种情况都编写单元测试方法。具体请看上述代码。</p>
<p>单元测试代码编写好之后可以先运行。在<code>CoverageTest.java</code>上右键 -&gt; 选择<code>Coverage As</code> -&gt; 选择<code>Junit Test</code>。<br>执行完毕之后会有如下截图结果：</p>
<p><img src="/image/blog-image/18120602.png" alt="运行结果"></p>
<p>打开Coverage视图，可以看到我们的代码达到了100%的覆盖率。也就是说我们的代码中所有分子都已经被测试通过。</p>
<p><img src="/image/blog-image/18120603.png" alt="运行结果2"></p>
<h3 id="自动执行单元测试"><a href="#自动执行单元测试" class="headerlink" title="自动执行单元测试"></a>自动执行单元测试</h3><p>上述右键使用<code>Coverage As</code>运行一个测试类，如果有多个测试的情况要如何处理呢？可以借助于<a target="_blank" rel="noopener" href="https://ant.apache.org/">ant</a>。使用ant自动编译java代码。</p>
<p>在项目根目录下创建<code>build.xml</code>，文件内容如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;project name=&quot;AntTestReporting&quot; basedir=&quot;.&quot; default=&quot;compile&quot;&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;!-- general --&gt;</span><br><span class="line">    &lt;property name=&quot;DEBUG&quot; value=&quot;true&quot; /&gt;</span><br><span class="line">    &lt;property name=&quot;VERBOSE&quot; value=&quot;true&quot; /&gt;</span><br><span class="line">    &lt;property name=&quot;TARGET&quot; value=&quot;1.8&quot; /&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;!-- folder --&gt;</span><br><span class="line">    &lt;property name=&quot;build.dir&quot; value=&quot;build&quot; /&gt;</span><br><span class="line">    &lt;property name=&quot;src.dir&quot; value=&quot;src/java&quot; /&gt;</span><br><span class="line">    &lt;property name=&quot;src.classess.dir&quot; value=&quot;$&#123;build.dir&#125;/classess&quot; /&gt;</span><br><span class="line">    &lt;property name=&quot;test.dir&quot; value=&quot;test/java&quot; /&gt;</span><br><span class="line">    &lt;property name=&quot;test.classess.dir&quot; value=&quot;$&#123;build.dir&#125;/test-classess&quot; /&gt;</span><br><span class="line">    &lt;property name=&quot;third-party-lib&quot; value=&quot;jacocolib&quot; /&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;!-- classpath --&gt;</span><br><span class="line">    &lt;path id=&quot;classpath&quot;&gt;</span><br><span class="line">        &lt;fileset dir=&quot;$&#123;third-party-lib&#125;&quot;&gt;</span><br><span class="line">            &lt;include name=&quot;**/*.jar&quot; /&gt;</span><br><span class="line">        &lt;/fileset&gt;</span><br><span class="line">    &lt;/path&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;!-- targets --&gt;</span><br><span class="line">    &lt;target name=&quot;clean&quot;&gt;</span><br><span class="line">        &lt;delete dir=&quot;$&#123;build.dir&#125;&quot; /&gt;</span><br><span class="line">    &lt;/target&gt;</span><br><span class="line">    &lt;target name=&quot;init&quot;&gt;</span><br><span class="line">        &lt;mkdir dir=&quot;$&#123;build.dir&#125;&quot; /&gt;</span><br><span class="line">        &lt;mkdir dir=&quot;$&#123;src.classess.dir&#125;&quot; /&gt;</span><br><span class="line">        &lt;mkdir dir=&quot;$&#123;test.classess.dir&#125;&quot; /&gt;</span><br><span class="line">    &lt;/target&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;target name=&quot;compile&quot; depends=&quot;clean, init&quot;&gt;</span><br><span class="line">        &lt;!-- Create the time stamp --&gt;</span><br><span class="line">        &lt;tstamp&gt;</span><br><span class="line">            &lt;format property=&quot;lastUpdated&quot; pattern=&quot;yyyy-MM-dd HH:mm:ss&quot; /&gt;</span><br><span class="line">        &lt;/tstamp&gt;</span><br><span class="line"> </span><br><span class="line">         &lt;!-- 编译java代码 --&gt;</span><br><span class="line">        &lt;javac target=&quot;$&#123;TARGET&#125;&quot; debug=&quot;$&#123;DEBUG&#125;&quot; verbose=&quot;$&#123;VERBOSE&#125;&quot; classpathref=&quot;classpath&quot;</span><br><span class="line">            optimize=&quot;true&quot; destdir=&quot;$&#123;src.classess.dir&#125;&quot; srcdir=&quot;$&#123;src.dir&#125;&quot;&gt;</span><br><span class="line">        &lt;/javac&gt;</span><br><span class="line">        &lt;echo&gt;======== java代码编译完毕 ========&lt;/echo&gt;</span><br><span class="line">        </span><br><span class="line">        &lt;path id=&quot;classpath.src.dir&quot;&gt;</span><br><span class="line">            &lt;pathelement location=&quot;$&#123;src.classess.dir&#125;&quot; /&gt;</span><br><span class="line">            &lt;!-- 引入第三方jar --&gt;</span><br><span class="line">            &lt;fileset dir=&quot;$&#123;third-party-lib&#125;&quot;&gt;</span><br><span class="line">                &lt;include name=&quot;**/*.jar&quot; /&gt;</span><br><span class="line">            &lt;/fileset&gt;</span><br><span class="line">        &lt;/path&gt;</span><br><span class="line">         &lt;!-- 编译test代码 --&gt;</span><br><span class="line">        &lt;javac target=&quot;$&#123;TARGET&#125;&quot; debug=&quot;$&#123;DEBUG&#125;&quot; verbose=&quot;$&#123;VERBOSE&#125;&quot;</span><br><span class="line">            optimize=&quot;true&quot; destdir=&quot;$&#123;test.classess.dir&#125;&quot; srcdir=&quot;$&#123;test.dir&#125;&quot;&gt;</span><br><span class="line">            &lt;!-- test代码依赖src的代码和第三方jar --&gt;</span><br><span class="line">            &lt;classpath refid=&quot;classpath.src.dir&quot; /&gt;</span><br><span class="line">        &lt;/javac&gt;</span><br><span class="line">         &lt;echo&gt;======== test代码编译完毕 ========&lt;/echo&gt;</span><br><span class="line">    &lt;/target&gt;</span><br><span class="line"> </span><br><span class="line">&lt;/project&gt;</span><br></pre></td></tr></table></figure>

<p>这个脚本主要做了两件事：</p>
<ul>
<li>编译<code>src/java</code>目录下面的java代码到目录<code>build/classess</code>下。</li>
<li>编译<code>test/java</code>目录下面的java代码到目录<code>build/test-classess</code>下。 </li>
</ul>
<p>在这个文件上右键 -&gt; <code>Run As</code> -&gt; <code>Ant Build</code>。执行完毕后，刷新项目。可以看到在项目根目录下自动创建了一个名为<code>build</code>的目录。展开之后可以看到项目的所有class文件。</p>
<p><img src="/image/blog-image/18120604.png" alt="编译结果1"></p>
<h3 id="自动生成覆盖率报告"><a href="#自动生成覆盖率报告" class="headerlink" title="自动生成覆盖率报告"></a>自动生成覆盖率报告</h3><p>编译之后得到你的业务代码class文件和单元测试的class文件。那么如何批量运行这些class呢？<br>同样的也是使用Ant。</p>
<p>在项目根目录下创建<code>build-test.xml</code>，文件内容如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;project name=&quot;AntTestReporting-test&quot; basedir=&quot;.&quot; default=&quot;all-test&quot; xmlns:jacoco=&quot;antlib:org.jacoco.ant&quot;&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;import file=&quot;build.xml&quot; /&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;!-- Java Code Coverage --&gt;</span><br><span class="line">    &lt;taskdef uri=&quot;antlib:org.jacoco.ant&quot; resource=&quot;org/jacoco/ant/antlib.xml&quot;&gt;</span><br><span class="line">        &lt;classpath path=&quot;jacocolib/jacocoant.jar&quot; /&gt;</span><br><span class="line">    &lt;/taskdef&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;property name=&quot;reports.dir&quot; value=&quot;$&#123;basedir&#125;/reports&quot; /&gt;</span><br><span class="line">    &lt;property name=&quot;test.data.dir&quot; value=&quot;$&#123;reports.dir&#125;/testResults&quot; /&gt;</span><br><span class="line">    &lt;property name=&quot;coverage.reports.dir&quot; value=&quot;$&#123;reports.dir&#125;/coverage&quot; /&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;property name=&quot;xms&quot; value=&quot;-Xms256m&quot; /&gt;</span><br><span class="line">    &lt;property name=&quot;xmx&quot; value=&quot;-Xmx1024m&quot; /&gt;</span><br><span class="line">    &lt;!-- &lt;property name=&quot;log4j.config&quot; value=&quot;-Dlog4j.configuration=file:/$&#123;base.dir&#125;/test/log4j-test.properties&quot; /&gt; --&gt;</span><br><span class="line">    </span><br><span class="line">    &lt;!-- test代码类路径，包括src代码、测试代码本身、jacoco jar --&gt;    </span><br><span class="line">     &lt;path id=&quot;classpath.test.dir&quot;&gt;</span><br><span class="line">         &lt;!-- 测试代码依赖src代码，需要作为test代码的运行classpath --&gt;</span><br><span class="line">        &lt;path refid=&quot;classpath.src.dir&quot;/&gt;</span><br><span class="line">        &lt;pathelement location=&quot;$&#123;test.classess.dir&#125;&quot; /&gt;</span><br><span class="line">    &lt;/path&gt;</span><br><span class="line">    </span><br><span class="line">    &lt;target name=&quot;init-report-dir&quot;&gt;</span><br><span class="line">        &lt;delete dir=&quot;$&#123;test.data.dir&#125;&quot; /&gt;</span><br><span class="line">        &lt;delete dir=&quot;$&#123;coverage.reports.dir&#125;&quot; /&gt;</span><br><span class="line">        &lt;delete dir=&quot;$&#123;reports.dir&#125;&quot; /&gt;</span><br><span class="line">        </span><br><span class="line">        &lt;mkdir dir=&quot;$&#123;reports.dir&#125;&quot; /&gt;</span><br><span class="line">        &lt;mkdir dir=&quot;$&#123;test.data.dir&#125;&quot; /&gt;</span><br><span class="line">        &lt;mkdir dir=&quot;$&#123;coverage.reports.dir&#125;&quot; /&gt;</span><br><span class="line">    &lt;/target&gt;</span><br><span class="line">        </span><br><span class="line">    &lt;target name=&quot;test&quot; depends=&quot;init-report-dir, compile&quot;&gt;</span><br><span class="line">        &lt;echo&gt;========= 运行所有test类 =========&lt;/echo&gt;</span><br><span class="line">        &lt;jacoco:coverage destfile=&quot;$&#123;test.data.dir&#125;/jacoco.exec&quot;&gt;</span><br><span class="line">            &lt;junit printsummary=&quot;true&quot; haltonfailure=&quot;false&quot; fork=&quot;yes&quot; forkmode=&quot;once&quot;&gt;</span><br><span class="line">                &lt;jvmarg value=&quot;$&#123;xms&#125;&quot; /&gt;</span><br><span class="line">                &lt;jvmarg value=&quot;$&#123;xmx&#125;&quot; /&gt;</span><br><span class="line">                &lt;classpath refid=&quot;classpath.test.dir&quot; /&gt;</span><br><span class="line">                &lt;formatter type=&quot;xml&quot; /&gt;</span><br><span class="line">                &lt;!-- 执行 *Test类 --&gt;</span><br><span class="line">                &lt;batchtest todir=&quot;$&#123;test.data.dir&#125;&quot;&gt;</span><br><span class="line">                    &lt;fileset dir=&quot;$&#123;test.classess.dir&#125;&quot;&gt;</span><br><span class="line">                        &lt;!-- Exclude inner classes --&gt;</span><br><span class="line">                        &lt;exclude name=&quot;**/*$*.class&quot; /&gt;</span><br><span class="line">                        &lt;include name=&quot;**/*Test.class&quot; /&gt;</span><br><span class="line">                    &lt;/fileset&gt;</span><br><span class="line">                &lt;/batchtest&gt;</span><br><span class="line">            &lt;/junit&gt;</span><br><span class="line">        &lt;/jacoco:coverage&gt;</span><br><span class="line"> </span><br><span class="line">        &lt;!-- Generate HTML report</span><br><span class="line">            - junit-noframes.html -&gt; Single page HTML-report</span><br><span class="line">            - index.html -&gt; HTML-report using frames (several files, but more comfortable to read)--&gt;</span><br><span class="line">        &lt;echo&gt;========= 生成Junit报告 =========&lt;/echo&gt;</span><br><span class="line">        &lt;junitreport todir=&quot;$&#123;test.data.dir&#125;&quot;&gt;</span><br><span class="line">            &lt;fileset dir=&quot;$&#123;test.data.dir&#125;&quot;&gt;</span><br><span class="line">                &lt;include name=&quot;TEST-*.xml&quot; /&gt;</span><br><span class="line">            &lt;/fileset&gt;</span><br><span class="line">        &lt;/junitreport&gt;</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">        &lt;!-- Generate Code Coverage report</span><br><span class="line">            See: http://www.eclemma.org/jacoco/trunk/doc/ant.html --&gt;</span><br><span class="line">        &lt;echo&gt;========= 生成覆盖率报告 =========&lt;/echo&gt;    </span><br><span class="line">        &lt;jacoco:report&gt;</span><br><span class="line">            &lt;executiondata&gt;</span><br><span class="line">                &lt;file file=&quot;$&#123;test.data.dir&#125;/jacoco.exec&quot; /&gt;</span><br><span class="line">            &lt;/executiondata&gt;</span><br><span class="line"> </span><br><span class="line">            &lt;structure name=&quot;AntTestReporting&quot;&gt;</span><br><span class="line">                &lt;classfiles&gt;</span><br><span class="line">                    &lt;fileset dir=&quot;$&#123;src.classess.dir&#125;&quot;&gt;</span><br><span class="line">                        &lt;include name=&quot;**/*.class&quot; /&gt;</span><br><span class="line">                        &lt;!-- 生成的报告中排除test类 --&gt;</span><br><span class="line">                        &lt;!-- &lt;exclude name=&quot;**/*Test*.class&quot; /&gt; --&gt;</span><br><span class="line">                        &lt;!-- Exclude inner classes --&gt;</span><br><span class="line">                        &lt;exclude name=&quot;**/*$*.class&quot; /&gt;</span><br><span class="line">                    &lt;/fileset&gt;</span><br><span class="line">                &lt;/classfiles&gt;</span><br><span class="line">            &lt;/structure&gt;</span><br><span class="line"> </span><br><span class="line">            &lt;html destdir=&quot;$&#123;coverage.reports.dir&#125;&quot; /&gt;</span><br><span class="line">        &lt;/jacoco:report&gt;</span><br><span class="line">    &lt;/target&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;target name=&quot;all-test&quot; depends=&quot;test&quot; /&gt;</span><br><span class="line">&lt;/project&gt;</span><br></pre></td></tr></table></figure>

<p>这个脚本主要做如下三件事：</p>
<h4 id="1-运行所有test类"><a href="#1-运行所有test类" class="headerlink" title="1. 运行所有test类"></a>1. 运行所有test类</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&lt;jacoco:coverage destfile=&quot;$&#123;test.data.dir&#125;/jacoco.exec&quot;&gt;</span><br><span class="line">    &lt;junit printsummary=&quot;true&quot; haltonfailure=&quot;false&quot; fork=&quot;yes&quot; forkmode=&quot;once&quot;&gt;</span><br><span class="line">        &lt;jvmarg value=&quot;$&#123;xms&#125;&quot; /&gt;</span><br><span class="line">        &lt;jvmarg value=&quot;$&#123;xmx&#125;&quot; /&gt;</span><br><span class="line">        &lt;classpath refid=&quot;classpath.test.dir&quot; /&gt;</span><br><span class="line">        &lt;formatter type=&quot;xml&quot; /&gt;</span><br><span class="line">        &lt;!-- 执行 build/test-classess下面的所有class --&gt;</span><br><span class="line">        &lt;batchtest todir=&quot;$&#123;test.data.dir&#125;&quot;&gt;</span><br><span class="line">            &lt;fileset dir=&quot;$&#123;test.classess.dir&#125;&quot;&gt;</span><br><span class="line">                &lt;include name=&quot;**/*Test.class&quot; /&gt;</span><br><span class="line">            &lt;/fileset&gt;</span><br><span class="line">        &lt;/batchtest&gt;</span><br><span class="line">    &lt;/junit&gt;</span><br><span class="line">&lt;/jacoco:coverage&gt;</span><br></pre></td></tr></table></figure>

<p>这段代码就是用于执行Junit方法的，有关Junit标签详细使用说明请移步<a target="_blank" rel="noopener" href="https://ant.apache.org/manual/">https://ant.apache.org/manual/</a>。<br>通过标签<code>batchtest</code>指定了执行的范围，<code>dir</code>属性指定运行的class文件放在<code>build/test-classess</code>。这个目录下有可能有其他的文件所以使用<code>include</code>标签指定只执行名字后缀是<code>Test</code>的class文件。<br>这段脚本执行完毕之后会在<code>reports/testResults</code>得到一个exec文件。这些文件和目录也是脚本自动创建的。</p>
<h4 id="2-生成Junit报告"><a href="#2-生成Junit报告" class="headerlink" title="2. 生成Junit报告"></a>2. 生成Junit报告</h4><p>经过第一点的脚本之后，根据执行结果生成Junit报告。报告保存在<code>reports/testResults</code>。这部分是可选的，如果你不想要Junit报告可以删除这段脚本。删除之后再执行在<code>reports/testResults</code>目录下面就不会有<code>TESTS-TestSuites.xml</code>。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;junitreport todir=&quot;$&#123;test.data.dir&#125;&quot;&gt;</span><br><span class="line">    &lt;fileset dir=&quot;$&#123;test.data.dir&#125;&quot;&gt;</span><br><span class="line">        &lt;include name=&quot;TEST-*.xml&quot; /&gt;</span><br><span class="line">    &lt;/fileset&gt;</span><br><span class="line">&lt;/junitreport&gt;</span><br></pre></td></tr></table></figure>

<h4 id="3-生成覆盖率报告"><a href="#3-生成覆盖率报告" class="headerlink" title="3. 生成覆盖率报告"></a>3. 生成覆盖率报告</h4><p>经过前面两步之后，可以根据前面两步的结果生成覆盖率的报告。报告所依赖的数据就是<code>&lt;file file=&quot;$&#123;test.data.dir&#125;/jacoco.exec&quot; /&gt;</code>。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;jacoco:report&gt;</span><br><span class="line">    &lt;executiondata&gt;</span><br><span class="line">        &lt;file file=&quot;$&#123;test.data.dir&#125;/jacoco.exec&quot; /&gt;</span><br><span class="line">    &lt;/executiondata&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;structure name=&quot;AntTestReporting&quot;&gt;</span><br><span class="line">        &lt;classfiles&gt;</span><br><span class="line">            &lt;fileset dir=&quot;$&#123;src.classess.dir&#125;&quot;&gt;</span><br><span class="line">                &lt;include name=&quot;**/*.class&quot; /&gt;</span><br><span class="line">                &lt;!-- 生成的报告中排除test类 --&gt;</span><br><span class="line">                &lt;!-- &lt;exclude name=&quot;**/*Test*.class&quot; /&gt; --&gt;</span><br><span class="line">                &lt;!-- Exclude inner classes --&gt;</span><br><span class="line">                &lt;exclude name=&quot;**/*$*.class&quot; /&gt;</span><br><span class="line">            &lt;/fileset&gt;</span><br><span class="line">        &lt;/classfiles&gt;</span><br><span class="line">    &lt;/structure&gt;</span><br><span class="line"> </span><br><span class="line">    &lt;html destdir=&quot;$&#123;coverage.reports.dir&#125;&quot; /&gt;</span><br><span class="line">&lt;/jacoco:report&gt;</span><br></pre></td></tr></table></figure>

<p>最终，整个脚本执行完毕之后，在项目根目录下可以看到如下截图内容：</p>
<p><img src="/image/blog-image/18120605.png" alt="覆盖率报告"></p>
<p>用浏览器打开<code>index.html</code>，在这里文件中可以看到项目所有代码的覆盖率情况。</p>
<p><img src="/image/blog-image/18120606.png" alt="覆盖率报告2"></p>
<p>报告单结果和你直接使用<code>Coverage As</code>运行的结果是一致的。如果别人需要项目的结果你就可以把<code>reports</code>目录发给他了，这个就是项目的单元覆盖率结果。</p>
<h3 id="项目源码"><a href="#项目源码" class="headerlink" title="项目源码"></a>项目源码</h3><p><a target="_blank" rel="noopener" href="https://github.com/ubuntuvim/coverage">https://github.com/ubuntuvim/coverage</a></p>
<p>后续版本会继承spring，针对spring或者是springMVC项目又是如何做的呢？？</p>

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/junit-jacoco/spring-gradle-%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E6%B5%8B%E8%AF%95/" rel="next" title="spring-gradle-代码覆盖测试">
          spring-gradle-代码覆盖测试
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/hexo/%E5%A6%82%E4%BD%95%E4%BB%8E%E6%A0%B9%E6%9C%AC%E8%A7%A3%E5%86%B3hexo%E4%B8%8D%E5%85%BC%E5%AE%B9%7B%7B%7D%7D%E6%A0%87%E7%AD%BE%E9%97%AE%E9%A2%98/" rel="prev" title="如何从根本解决hexo不兼容{{}}标签问题">
            如何从根本解决hexo不兼容{{}}标签问题
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-text">准备工作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%80%E9%9C%80%E8%BD%AF%E4%BB%B6%E3%80%81jar"><span class="toc-text">所需软件、jar</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BAjava%E9%A1%B9%E7%9B%AE"><span class="toc-text">创建java项目</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A2%9E%E5%8A%A0%E4%BE%9D%E8%B5%96jar"><span class="toc-text">增加依赖jar</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E5%86%99%E7%AE%80%E5%8D%95%E7%9A%84java%E4%BB%A3%E7%A0%81"><span class="toc-text">编写简单的java代码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E5%86%99%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95"><span class="toc-text">编写单元测试</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%8A%A8%E6%89%A7%E8%A1%8C%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95"><span class="toc-text">自动执行单元测试</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E8%A6%86%E7%9B%96%E7%8E%87%E6%8A%A5%E5%91%8A"><span class="toc-text">自动生成覆盖率报告</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E8%BF%90%E8%A1%8C%E6%89%80%E6%9C%89test%E7%B1%BB"><span class="toc-text">1. 运行所有test类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E7%94%9F%E6%88%90Junit%E6%8A%A5%E5%91%8A"><span class="toc-text">2. 生成Junit报告</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E7%94%9F%E6%88%90%E8%A6%86%E7%9B%96%E7%8E%87%E6%8A%A5%E5%91%8A"><span class="toc-text">3. 生成覆盖率报告</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81"><span class="toc-text">项目源码</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: 'junit-jacoco代码覆盖测试', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
